/**
 * Created by beautiful luna on 2016/5/25.
 */


import com.rabbit.myblog.entity.Blog;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

public class BlogsListXmlParser {


    /**
     * 用于解析博客列表的xml，返回Blog的List集合对象
     *
     * @param inputStream
     * @param encode
     * @return
     * @throws XmlPullParserException
     * @throws IOException
     */
    public static List<Blog> getListBlogs(InputStream inputStream, String encode) throws XmlPullParserException, IOException {

        List<Blog> mBlogs = null;
        Blog mBlog = null;

        //获取XmlPullParser实例
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        XmlPullParser parser = factory.newPullParser();
        parser.setInput(inputStream, encode);
        //获取解析事件
        int eventType = parser.getEventType();
        //当xml文档未到尾端时
        while (eventType != XmlPullParser.END_DOCUMENT) {
            switch (eventType) {
                //解析根标签的时候，实例化集合
                case XmlPullParser.START_DOCUMENT:
                    mBlogs = new ArrayList<Blog>();
                    mBlog = new Blog();

                    break;
                case XmlPullParser.START_TAG:
                    //当解析到entry标签的时候，实例化Blog对象
                    if ("entry".equals(parser.getName())) {
                        mBlog = new Blog();
                    }
                    if ("id".equals(parser.getName())) {
                        parser.next();
                        mBlog.setBlogId(parser.getText());
                    } else if ("title".equals(parser.getName())) {
                        parser.next();
                        //特殊处理
                        if (!"博客园".equals(parser.getText())) {
                            mBlog.setBlogTitle(parser.getText());
                        }
                    } else if ("summary".equals(parser.getName())) {
                        parser.next();
                        mBlog.setBlogSummary(parser.getText());
                    } else if ("published".equals(parser.getName())) {
                        parser.next();
                        mBlog.setBlogPublished(parser.getText());
                    } else if ("name".equals(parser.getName())) {
                        parser.next();
                        mBlog.setAuthorName(parser.getText());
                    } else if ("uri".equals(parser.getName())) {
                        parser.next();
                        mBlog.setAuthorUri(parser.getText());
                    } else if ("avatar".equals(parser.getName())) {
                        parser.next();
                        mBlog.setAuthorAvatar(parser.getText());
                    } else if ("link".equals(parser.getName())) {
                        //特殊处理
                        if (parser.getAttributeName(0).equals("rel")) {
                            mBlog.setBlogLink(parser.getAttributeValue(1));
                        }
                    } else if ("diggs".equals(parser.getName())) {
                        parser.next();
                        mBlog.setBlogDiggs(parser.getText());
                    } else if ("views".equals(parser.getName())) {
                        parser.next();
                        mBlog.setBlogViews(parser.getText());
                    } else if ("comments".equals(parser.getName())) {
                        parser.next();
                        mBlog.setBlogComments(parser.getText());
                    }
                    break;
                case XmlPullParser.END_TAG:
                    //当解析到entry标签结束的时候添加入Blogs集合，清空Blog对象
                    if ("entry".equals(parser.getName())) {
                        mBlogs.add(mBlog);
                        mBlog = null;
                    }
                    break;

            }
            //手动跳转第一次遍历
            eventType = parser.next();
        }


        return mBlogs;

    }

}
